//package com.sztz.filter;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import com.sztz.constants.CommonConstants;
//import com.sztz.service.impl.MyUserDetailServiceImpl;
//import com.sztz.utils.JwtTokenUtils;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * Token校验过滤器
// * @author yzy
// * @date 2023/3/21 19:10
// */
//public class TokenAuthenticationFilter extends OncePerRequestFilter {
//    private final MyUserDetailServiceImpl myUserDetailService;
//    public TokenAuthenticationFilter(MyUserDetailServiceImpl myUserDetailService){
//        this.myUserDetailService = myUserDetailService;
//    }
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getHeader(CommonConstants.TOKEN_HEADER);
//        if(StrUtil.isNotBlank(token)){
//            String username = JwtTokenUtils.getUsernameFromToken(token);
//            //如果token未认证过就需要认证
//            if(StrUtil.isNotBlank(username) && ObjectUtil.isNull(SecurityContextHolder.getContext().getAuthentication())           ){
//                UserDetails userDetails = myUserDetailService.loadUserByUsername(username);
//                if(JwtTokenUtils.validateToken(token,userDetails)){
//                    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
//                    authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                    //token存到ThreadLocal里,方便下次直接使用
//                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//                }
//            }
//        }
//        //继续执行下一个过滤器
//        filterChain.doFilter(request,response);
//    }
//}
